﻿using System;
using System.Globalization;
using System.Resources;
using System.Windows.Forms;
using TSites.GUI.Enums;
using TSites.GUI.Languages;
using VNMessageBox;

namespace TSites.GUI
{
	public partial class frmImmanuelInfo : Form
	{
		public frmImmanuelInfo()
		{
			InitializeComponent();
		}

		#region Methods

		public void ApplyResource()
		{
			CultureInfo culture = CultureInfoFactory.GetInstance();
			ResourceManager rm = new ResourceManager(CultureInfoFactory.BASEDNAME, typeof(frmImmanuelInfo).Assembly);

			lblVNName.Text = rm.GetString("ImmaVNName", culture) + ":";
			lblENName.Text = rm.GetString("ImmaENName", culture) + ":";
			lblRepresentedBy.Text = rm.GetString("ImmaRepresentedBy", culture) + ":";
			lblVNPosition.Text = rm.GetString("ImmaVNPosition", culture) + ":";
			lblENPosition.Text = rm.GetString("ImmaENPosition", culture) + ":";
			lblAuthorizationNo.Text = rm.GetString("ImmaAuthorizationNo", culture) + ":";
			lblDate.Text = rm.GetString("ImmaAuthorizationDate", culture) + ":";
			lblPhone.Text = rm.GetString("ImmaTelephoneNo", culture) + ":";
			lblFax.Text = rm.GetString("ImmaFax", culture) + ":";
			lblVNAddress.Text = rm.GetString("ImmaVNAddress", culture) + ":";
			lblENAddress.Text = rm.GetString("ImmaENAddress", culture) + ":";
			lblBeneficiary.Text = rm.GetString("ImmaBeneficiary", culture) + ":";
			lblBankAccount.Text = rm.GetString("ImmaBankAccount", culture) + ":";
			lblVNBankAccAt.Text = rm.GetString("ImmaVNBankAccountAt", culture) + ":";
			lblENBankAccAt.Text = rm.GetString("ImmaENBankAccountAt", culture) + ":";
			lblTaxCode.Text = rm.GetString("ImmaTaxCode", culture) + ":";

			btnSave.Text = rm.GetString("Save", culture);
		}

		private void FillGUI(Immanuelnfo immanuel)
		{
			if (immanuel != null)
			{
				txtVNName.Text = immanuel.VNName;
				txtENName.Text = immanuel.ENName;
				txtVNPosition.Text = immanuel.VNPosition;
				txtENPosition.Text = immanuel.ENPosition;
				txtRepresentedBy.Text = immanuel.RepresentedBy;
				txtAuthorizationNo.Text = immanuel.AuthorizationNo;
				dtpAuthorizationDate.Value = immanuel.AuthorizationDate;
				txtPhone.Text = immanuel.TelephoneNo;
				txtFax.Text = immanuel.Fax;
				txtVNAddress.Text = immanuel.VNAddress;
				txtENAddress.Text = immanuel.ENAddress;
				txtBeneficiary.Text = immanuel.Beneficiary;
				txtBankAccout.Text = immanuel.BankAccount;
				txtBankVNAt.Text = immanuel.BankVNAccountAt;
				txtBankENAt.Text = immanuel.BankENAccountAt;
				txtTaxCode.Text = immanuel.TaxCode;
			}
		}

		private void SaveImmanuelInformation()
		{
			Immanuelnfo immanuel = new Immanuelnfo
			{
				VNName = txtVNName.Text.Trim(),
				ENName = txtENName.Text.Trim(),
				RepresentedBy = txtRepresentedBy.Text.Trim(),
				VNPosition = txtVNPosition.Text.Trim(),
				ENPosition = txtENPosition.Text.Trim(),
				AuthorizationNo = txtAuthorizationNo.Text.Trim(),
				AuthorizationDate = dtpAuthorizationDate.Value.Date,
				TelephoneNo = txtPhone.Text.Trim(),
				Fax = txtFax.Text.Trim(),
				VNAddress = txtVNAddress.Text.Trim(),
				ENAddress = txtENAddress.Text.Trim(),
				Beneficiary = txtBeneficiary.Text.Trim(),
				BankAccount = txtBankAccout.Text.Trim(),
				BankVNAccountAt = txtBankVNAt.Text.Trim(),
				BankENAccountAt = txtBankENAt.Text.Trim(),
				TaxCode = txtTaxCode.Text.Trim()
			};

			LANGUAGE language;
			CultureInfo culture;
			ResourceManager rm;
			string title;
			try
			{
				if (ConfigurationSingleton.Instance.SaveImmanuelInfo(immanuel))
				{
					culture = CultureInfoFactory.GetInstance();
					rm = new ResourceManager(CultureInfoFactory.BASEDNAME, typeof(frmImmanuelInfo).Assembly);
					string msg = rm.GetString("SaveImmaSuccess", culture);
					title = rm.GetString("Notice", culture);

					language = ConfigurationSingleton.Instance.Language;
					if (language == LANGUAGE.Vietnames)
					{
						CMessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
					}
					else
					{
						MessageBox.Show(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
					}
				}
			}
			catch (Exception ex)
			{
				language = ConfigurationSingleton.Instance.Language;
				culture = CultureInfoFactory.GetInstance();
				rm = new ResourceManager(CultureInfoFactory.BASEDNAME, typeof(frmExcelImport).Assembly);

				title = rm.GetString("Error", culture);
				if (language == LANGUAGE.Vietnames)
				{
					CMessageBox.Show(ex.StackTrace, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				else
				{
					MessageBox.Show(ex.StackTrace, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		
		#endregion

		#region Events

		private void frmImmanuelInfo_Load(object sender, EventArgs e)
		{
			FillGUI(ConfigurationSingleton.Instance.Immanuel);
			ApplyResource();
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			SaveImmanuelInformation();
		}
		
		#endregion
	}
}
